package com.example.schedulecard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.schedulecard.Databases.ScheduleFestOption;
import com.example.schedulecard.Entity.Schedule;

public class AddSchedule extends AppCompatActivity implements View.OnClickListener {
    EditText sc;
    TextView add_name;
    ImageView back;
    Button reset, submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_schedule);
        Init();
    }

    private void Init() {
        sc = (EditText) findViewById(R.id.add_schedule_info);

        add_name = (TextView) findViewById(R.id.add_schedule1);
        add_name.setText("添加课程表");

        reset = (Button) findViewById(R.id.add_schedule_rebutton);
        submit = (Button) findViewById(R.id.add_schedule_inbutton);
        back = (ImageView) findViewById(R.id.add_schedule_back);

//        添加点击事件
        reset.setOnClickListener(this);
        submit.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_schedule_rebutton:
                sc.setText("");
                break;
            case R.id.add_schedule_inbutton:
                if ("".equals(sc.getText().toString())) {
                    Toast.makeText(AddSchedule.this, "课程名没有输入！", Toast.LENGTH_LONG).show();
                } else {
                    Schedule schedule = new Schedule(sc.getText().toString());
                    SQLiteDatabase sqLiteDatabase = ScheduleFestOption.
                            CreateScheduletable(AddSchedule.this).getWritableDatabase();
                    if (ScheduleFestOption.addSchedule(sqLiteDatabase, schedule) > 0) {
                        Toast.makeText(AddSchedule.this, "课程表创建成功，快去添加您的课程吧！",
                                Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(AddSchedule.this, MainActivity.class);
//                        设置活动栈的规则
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(AddSchedule.this, "课程表没有创建成功！请检查课程名是否有重复",
                                Toast.LENGTH_LONG).show();
                    }
                }
                break;
            case R.id.add_schedule_back:
                finish();
                break;
        }
    }
}